package org.arain.baseii.app.content;

import java.util.Date;
import java.util.List;

import org.apache.commons.lang.StringUtils;
import org.arain.baseii.app.navigation.NavigationDto;
import org.arain.baseii.app.navigation.NavigationService;
import org.arain.baseii.common.CommonServiceImpl;
import org.arain.baseii.grid.GridData;
import org.arain.baseii.grid.QueryParam;
import org.arain.baseii.mapper.BlogContentMapper;
import org.arain.baseii.pojo.BlogContent;
import org.arain.baseii.pojo.BlogContentExample;
import org.arain.baseii.pojo.BlogContentExample.Criteria;
import org.arain.baseii.utils.AppUtils;
import org.arain.baseii.utils.ArainResult;
import org.arain.baseii.utils.DictUtil;
import org.arain.baseii.utils.TimeUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.github.pagehelper.PageHelper;

@Service
public class ContentServiceImpl extends CommonServiceImpl<ContentDto> implements ContentService{
	@Autowired
	private BlogContentMapper blogContentMapper;
	@Autowired
	private NavigationService navigationService;
	@Override
	public ArainResult save_01(ContentDto dto) {
		if(StringUtils.isNotBlank(dto.getId())) {
			BlogContent content = new BlogContent();
			AppUtils.copyProperties(content, dto);
			content.setUpdateTime(new Date());
			blogContentMapper.updateByPrimaryKeyWithBLOBs(content);
			return ArainResult.ok();
		} else {
			BlogContent content = new BlogContent();
			AppUtils.copyProperties(content, dto);
			content.setCreateTime(new Date());
			blogContentMapper.insert(content);
			return ArainResult.ok();
		}
	}

	@Override
	public String load_grid(QueryParam param) {
		ContentDto dto = new ContentDto();
		try {
			dto = this.jsonToObject(param.getFilterRules(), dto);
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		JSONArray array = new JSONArray();
		BlogContentExample example = new BlogContentExample();
		Criteria criteria = example.createCriteria();
		if(StringUtils.isNotBlank(param.getOrder()) && StringUtils.isNotBlank(param.getSort())) {
			example.setOrderByClause(param.getSort()+" "+param.getOrder());
		}
		if(StringUtils.isNotBlank(dto.getTitle())) {
			criteria.andTitleLike("%"+dto.getTitle()+"%");
		}
		if(StringUtils.isNotBlank(dto.getType())) {
			criteria.andTypeEqualTo(dto.getType());
		}
		PageHelper.startPage(param.getPage(), param.getRows());
		List<BlogContent> list = blogContentMapper.selectByExample(example);
		int countByExample = blogContentMapper.countByExample(example);
		for (BlogContent content : list) {
			JSONObject object = new JSONObject();
			object.put("id", content.getId());
			object.put("title", content.getTitle());
			object.put("type", navigationService.load_01(new NavigationDto(content.getType())).getTitle());
			object.put("time", TimeUtil.format(content.getCreateTime()));
			object.put("recommend", DictUtil.load_dict(content.getRecommend()+"", "recommend").getDispaly());
			object.put("enabled",  DictUtil.load_dict(content.getEnabled()+"", "ENABLED_").getDispaly());
			
			array.add(object);
		}
		GridData data = new GridData();
		data.setTotal(countByExample);
		data.setRows(array);
		return GridData.gridToJson(data);
	}

	@Override
	public ContentDto load_01(ContentDto dto) {
		BlogContent content = blogContentMapper.selectByPrimaryKey(Long.valueOf(dto.getId()));
		AppUtils.copyProperties(dto, content);
		return dto;
	}
}
